package sandbox.session;

import org.apache.log4j.Logger;

import sandbox.vaadin.skiller.MutableLocalEntityProviderSkillSSB;
import sandbox.vaadin.skiller.MutableLocalEntityProviderSkillgroupSSB;
import sandbox.vaadin.skiller.entity.SkillgroupDAOSSB;

//import com.google.common.eventbus.EventBus;

public class DefaultSessionContext
    extends AbstractSessionContext
{
	private final static Logger LOGGER = Logger.getLogger(DefaultSessionContext.class);

//	/**
//	 * event bus for loosely coupled component communication
//	 */
//	private EventBus eventBus;
	
	/**
	 * Stateless session bean instance injected by {@link #setSkillgroupDAO(SkillgroupDAOSSB)}.
	 */
	private SkillgroupDAOSSB skillgroupDAO;

//	/**
//	 * Stateless session bean instance injected by {@link #setSkillDAO(SkillDAOSSB)}.
//	 */
//	private SkillDAOSSB skillDAO;

	/**
	 * Stateless session bean instance injected by {@link
	 * #setSkillgroupEntityProvider(MutableLocalEntityProviderSkillgroupSSB)}.
	 */
	private MutableLocalEntityProviderSkillgroupSSB skillgroupEntityProvider;

	/**
	 * Stateless session bean instance injected by {@link
	 * #setSkillEntityProvider(MutableLocalEntityProviderSkillSSB)}.
	 */
	private MutableLocalEntityProviderSkillSSB skillEntityProvider;

//	/**
//	 * name of authenticated user, <code>null</code> if no authenticated
//	 * user is logged in for the current session
//	 */
//	private String username;

	public DefaultSessionContext()
	{
		super();
	}

	public static DefaultSessionContext get()
	{
		Object result = AbstractSessionContext.get();
		
		if (result == null)
		{
			throw new IllegalStateException("no default session context data");
		}
		
		if (false == (result instanceof DefaultSessionContext))
		{
			throw new IllegalStateException(
					"wrong type of default session context data: " + result.getClass().getName());
		}

		return (DefaultSessionContext) result;
	}

//	/**
//	 * @return see {@link #eventBus}
//	 */
//	public EventBus getEventBus()
//	{
//		if (eventBus == null)
//		{
//			eventBus = new EventBus();
//		}
//
//		return eventBus;
//	}
	
//	/**
//	 * @return {@link #username}
//	 */
//	public String getUsername()
//	{
//		return username;
//	}
//	
//	/**
//	 * @param username see {@link #username}
//	 */
//	public void setUsername(String username)
//	{
//		this.username = username;
//	}

	public SkillgroupDAOSSB getSkillgroupDAO()
	{
		if (skillgroupDAO == null)
		{
			throw new IllegalStateException("skillgroupDAO must not be null");
		}

		return skillgroupDAO;
	}

	public void setSkillgroupDAO(SkillgroupDAOSSB skillgroupDAO)
	{
		if (this.skillgroupDAO != null)
		{
			LOGGER.warn("skillgroupDAO was already set and will not be changed");
		}
		
		if (skillgroupDAO == null)
		{
			throw new IllegalArgumentException("skillgroupDAO must not be null");
		}

		this.skillgroupDAO = skillgroupDAO;
	}

	public MutableLocalEntityProviderSkillgroupSSB getSkillgroupEntityProvider()
	{
		if (skillgroupEntityProvider == null)
		{
			throw new IllegalStateException("skillgroupEntityProvider must not be null");
		}

		return skillgroupEntityProvider;
	}

	public void setSkillgroupEntityProvider(MutableLocalEntityProviderSkillgroupSSB skillgroupEntityProvider)
	{
		if (this.skillgroupEntityProvider != null)
		{
			LOGGER.warn("skillgroupEntityProvider was already set and will not be changed");
		}
		
		if (skillgroupEntityProvider == null)
		{
			throw new IllegalArgumentException("skillgroupEntityProvider must not be null");
		}

		this.skillgroupEntityProvider = skillgroupEntityProvider;
	}

	public MutableLocalEntityProviderSkillSSB getSkillEntityProvider()
	{
		if (skillEntityProvider == null)
		{
			throw new IllegalStateException("skillEntityProvider must not be null");
		}

		return skillEntityProvider;
	}

	public void setSkillEntityProvider(MutableLocalEntityProviderSkillSSB skillEntityProvider)
	{
		if (this.skillEntityProvider != null)
		{
			LOGGER.warn("skillEntityProvider was already set and will not be changed");
		}
		
		if (skillEntityProvider == null)
		{
			throw new IllegalArgumentException("skillEntityProvider must not be null");
		}

		this.skillEntityProvider = skillEntityProvider;
	}
}